import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @author LY
 * @date 2022年02月23日 20:12
 * @project note
 * 按键检测：单击短按、双击、按下、释放
 * 单击长按
 */
public class TestKeyEvent extends JFrame implements KeyListener, MouseListener {
    Container con;
    JButton[][] jButton;
    JPanel jp;
    JTextArea textArea;
    JButton reset;
    private static String[] keys = {"1234567890-=", "QWERTYUIOP[]", "ASDFGHJKL'", "ZXCVBNM,./"};

    public TestKeyEvent() {
        super("键盘模拟器");
        this.draw();
    }

    private void draw() {
        con = this.getContentPane();
        con.setLayout(null);
        jButton = new JButton[keys.length][];
        for (int i = 0; i < keys.length; i++) {
            jButton[i] = new JButton[keys[i].length()];
            for (int j = 0; j < keys[i].length(); j++) {
                jButton[i][j] = new JButton(String.valueOf(keys[i].charAt(j)));
                jButton[i][j].setBounds(10 + j * 80, 30 + i * 80, 70, 70);
                jButton[i][j].setBackground(Color.BLUE);
                con.add(jButton[i][j]);
                jButton[i][j].addKeyListener(this);
                jButton[i][j].addMouseListener(this);
            }
        }
        textArea = new JTextArea();
        textArea.setBounds(20, 350, 900, 375);
        textArea.setEditable(false);
        con.add(textArea);
        reset = new JButton("重置");
        reset.setBounds(810, 190, 150, 150);
        reset.addMouseListener(this);
        con.add(reset);
        this.setSize(1000, 800);
        this.setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        

    }

    /**
     * 键盘按下，
     * 把指定按键变红
     */
    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("键盘按下:"+e.getKeyChar());
        for (int i = 0; i < keys.length; i++) {
            for (int j = 0; j < keys[i].length(); j++) {
                if (jButton[i][j].getText().equals(String.valueOf(Character.toUpperCase(e.getKeyChar())))) {
                    jButton[i][j].setBackground(Color.red);
                    textArea.setText(textArea.getText() + jButton[i][j].getText());
                }
            }
        }

    }

    /**
     * 键盘释放，
     * 把按下字母恢复到原来状态
     */
    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("键盘释放:"+e.getKeyChar());
        for (int i = 0; i < keys.length; i++) {
            for (int j = 0; j < keys[i].length(); j++) {
                if (jButton[i][j].getText().equals(String.valueOf(Character.toUpperCase(e.getKeyChar())))) {
                    jButton[i][j].setBackground(Color.blue);
                }
            }
        }
    }

    public static void main(String[] args) {
        new TestKeyEvent();
    }

    /**
     * 鼠标点击把内容放入文本框
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource().equals(reset)) {
            textArea.setText("");
        } else {
            if (e.getClickCount() == 1) {
                JButton button = (JButton) e.getSource();
                for (int i = 0; i < keys.length; i++) {
                    for (int j = 0; j < keys[i].length(); j++) {
                        if (jButton[i][j].getText().equals(button.getText())) {
                            textArea.setText(textArea.getText() + button.getText());
                        }
                    }
                }
                System.out.println("鼠标单击：" +e.getClickCount()+" "+ button.getText());
            }
            if (e.getClickCount() == 2) {
                JButton button = (JButton) e.getSource();
                for (int i = 0; i < keys.length; i++) {
                    for (int j = 0; j < keys[i].length(); j++) {
                        if (jButton[i][j].getText().equals(button.getText())) {
                            textArea.setText(textArea.getText() + button.getText());
                        }
                    }
                }
                System.out.println("鼠标双击：" + e.getClickCount()+" "+ button.getText());

            }
        }
    }
    /**
     * 鼠标按下
     */
    @Override
    public void mousePressed(MouseEvent e) {
        
        System.out.println("鼠标按下：" + e.getClickCount());
    }

    /**
     * 鼠标释放
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("鼠标释放：" + e.getClickCount());
    }

    /**
     * 鼠标进入相应组件
     * 按钮变红
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        
        JButton button = (JButton) e.getSource();
        for (int i = 0; i < keys.length; i++) {
            for (int j = 0; j < keys[i].length(); j++) {
                if (jButton[i][j].getText().equals(button.getText())) {
                    jButton[i][j].setBackground(Color.red);
                }
            }
        }
    }

    /**
     * 鼠标退出组件
     * 按钮恢复蓝色
     */
    @Override
    public void mouseExited(MouseEvent e) {
        JButton button = (JButton) e.getSource();
        for (int i = 0; i < keys.length; i++) {
            for (int j = 0; j < keys[i].length(); j++) {
                if (jButton[i][j].getText().equals(button.getText())) {
                    jButton[i][j].setBackground(Color.blue);
                }
            }
        }
    }
}

